package com.hexiang.client;

import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
import com.github.pagehelper.PageInfo;
import com.hexiang.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserHttpDegradeFallbackFactory implements FallbackFactory<UserHttpApiClient> {

    /**
     * Returns an instance of the fallback appropriate for the given cause
     *
     * @param cause fallback cause
     * @return 实现了retrofit接口的实例。an instance that implements the retrofit interface.
     */
    @Override
    public UserHttpApiClient create(Throwable cause) {
        log.error("触发熔断了! ", cause.getMessage(), cause);
        return new UserHttpApiClient() {
            @Override
            public List<User> getUsers() {
                return new ArrayList<>();
            }

            @Override
            public User getUserByName(String name) {
                return new User();
            }

            @Override
            public Integer addUser(Map<String, Object> params) {
                return -1;
            }

            @Override
            public Integer deleteUserById(Long id) {
                return -1;
            }

            @Override
            public Integer updateUser(User user) {
                return -1;
            }

            @Override
            public PageInfo getUserPage(Integer pageNum, Integer pageSize) {
                return new PageInfo();
            }

        };
    }
}